<template>
  <div class="system-role-container layout-padding">

    <div class="system-role-padding layout-padding-auto layout-padding-view">
      <div class="system-user-search mb15">
        <el-input size="default" v-model="state.keyword" placeholder="请输入关键字" style="max-width: 180px" clearable> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon> <ele-Search /> </el-icon>
          查  询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenDialog('add')">
          <el-icon><ele-CirclePlusFilled /></el-icon>
          新  增
        </el-button>
        <el-button size="default" type="danger" class="ml10" @click="onRowDel()">
          <el-icon><ele-DeleteFilled /></el-icon>
          删  除
        </el-button>
      </div>

      <el-table :data="state.tableData.data" ref="multipleTable" v-loading="state.tableData.loading" stripe style="width: 100%">
        <el-table-column type="selection" width="50" />
        <el-table-column type="index" label="序号" width="60" />
          <el-table-column prop="name" label="姓名" show-overflow-tooltip  width="120"></el-table-column>
        <el-table-column prop="sex" label="性别" show-overflow-tooltip  width="80">
          <template #default="scope">
            <span v-if="scope.row.sex==1">女</span>
            <span  v-else>男</span>
          </template>
        </el-table-column>
        <el-table-column prop="loginName" label="帐号名称" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column prop="loginPwd" label="帐号密码" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column prop="state" label="状态" show-overflow-tooltip width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.state==1">启用</el-tag>
            <el-tag type="info" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="部门" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column prop="deptName" label="角色" show-overflow-tooltip width="100"></el-table-column>

        <el-table-column prop="mobile" label="手机号码" show-overflow-tooltip  width="120"></el-table-column>
        <el-table-column prop="email" label="邮箱" show-overflow-tooltip  width="150"></el-table-column>
<!--        <el-table-column prop="idType" label="证件类型" show-overflow-tooltip  width="100"></el-table-column>-->
<!--        <el-table-column prop="idNo" label="证件号码" show-overflow-tooltip  width="100"></el-table-column>-->


        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenDialog('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="(val: number)=>{state.tableData.param.pageSize = val; getTableData();}"
          @current-change="(val: number) => {state.tableData.param.pageNum = val;getTableData();}"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </div>

    <div class="system-dic-dialog-container">
      <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable class="system-dialog">
        <el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
          <el-row :gutter="35">

           
             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="姓名">
                 <el-input v-model="state.ruleForm.name" placeholder="请输入姓名" clearable></el-input>
               </el-form-item>
             </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="状态">
                <el-switch v-model="state.ruleForm.state" :active-value="1" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
              </el-form-item>
            </el-col>
           
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="性别">
                <el-select v-model="state.ruleForm.sex" placeholder="请选择" clearable class="w100">
                  <el-option label="男" :value="0"></el-option>
                  <el-option label="女" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
           
             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="手机号码">
                 <el-input v-model="state.ruleForm.mobile" placeholder="请输入手机号码" clearable></el-input>
               </el-form-item>
             </el-col>


           
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="证件类型">
                <el-select v-model="state.ruleForm.idType" placeholder="请选择证件类型" clearable class="w100">
                  <el-option label="身份证" :value="0"></el-option>
                  <el-option label="护照" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="证件号码">
                 <el-input v-model="state.ruleForm.idNo" placeholder="请输入证件号码" clearable></el-input>
               </el-form-item>
             </el-col>
           
             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="帐号名称">
                 <el-input v-model="state.ruleForm.loginName" placeholder="请输入帐号名称" clearable></el-input>
               </el-form-item>
             </el-col>
           
             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="帐号密码">
                 <el-input v-model="state.ruleForm.loginPwd" placeholder="请输入帐号密码" clearable></el-input>
               </el-form-item>
             </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="部门">
                <el-cascader ref="deptTreeRef" v-model="state.ruleForm.deptId" placeholder="请选择部门" clearable
                             :options="state.dialog.deptOptions" collapse-tags filterable
                             :props="{multiple:true, checkStrictly:true, value:'id',label:'text' }"
                />
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="角色">
                <el-cascader ref="roleTreeRef" v-model="state.ruleForm.roleId" placeholder="请选择角色" clearable
                             :options="state.dialog.roleOptions" collapse-tags filterable
                             :props="{multiple:true, checkStrictly:true, value:'id',label:'text' }"
                />
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
              <el-form-item label="邮箱">
                <el-input v-model="state.ruleForm.email" placeholder="请输入邮箱" clearable></el-input>
              </el-form-item>
            </el-col>
         

          </el-row>
        </el-form>
        <template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialog.isShowDialog=false" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
        </template>
      </el-dialog>
    </div>

  </div>
</template>

<script setup lang="ts" name="systemDic">
import {reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import { api } from '/@/api/system/admin_user_api.ts';
import { api as dept_api } from '/@/api/system/admin_dept_api.ts';
import { api as role_api } from '/@/api/system/admin_role_api.ts';
import commonUtil from '/@/utils/commonUtil';

// 定义变量内容
const multipleTable = ref();
const deptTreeRef = ref();
const roleTreeRef = ref();
interface RowData {
    id?: string;
    operateType?: number;
   name?: string;
   sex?: number;
   state?: number;
   idType?: number;
   mobile?: string;
   email?: string;
   idNo?: string;
   loginName?: string;
   loginPwd?: string;
   wxOpenId?: string;
   wxSessionKey?: string;
   wxUserInfo?: string;
  deptId?: [] | null;
  roleId?: [] | null;

}
// 数据绑定
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
  keyword: '',
  ruleForm: <RowData>({}),
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
    deptOptions: [],
    roleOptions: [],
  },
});

//-------------------------------------------------------------------
// 打开弹窗
const onOpenDialog = (type: string, row ?: RowData) => {
  if (type === 'edit') {
    state.ruleForm = <RowData>(row);
    state.dialog.title = '修改用户信息';
    state.dialog.submitTxt = '修 改';
  } else {
    state.dialog.title = '新增用户信息';
    state.dialog.submitTxt = '新 增';
    state.ruleForm = <RowData>({});
  }
  state.dialog.isShowDialog = true;
};
// 提交
const onSubmit = () => {
  state.dialog.isShowDialog = false;
  //保存
  var data = state.ruleForm;
  var isAdd = data.id ? false : true;
  data.operateType = (isAdd ? 1 : 2);
  // 部门
  data.deptId = commonUtil.getCascaderValues(deptTreeRef);
  // 角色
  data.roleId = commonUtil.getCascaderValues(roleTreeRef);

  api().operate([data]).then((res)=>{
    if(commonUtil.hasErr(res, (isAdd?'新增':'修改') , true )) {return;}
    getTableData();
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};
// 删除
const onRowDel = (param ?: RowData) => {
  //获取选中的id
  let list: RowData[] = []
  if(param){
    list.push(param)
  }else{
    const selectionRows = multipleTable.value.getSelectionRows();
    if(!selectionRows || selectionRows.length == 0){
      ElMessage.error('请选择要操作的数据');
      return
    }
    list = selectionRows
  }
  // 设置值
  for (let i = 0; i < list.length; i++) {
    list[i].operateType=3; //删除
  }
  // 弹出框
  ElMessageBox.confirm(`是否永久删除 ?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    state.tableData.loading = true;
    api().operate(list).then((res)=>{
      if(commonUtil.hasErr(res, '删除' , true )) {return;}
        getTableData();
    }).catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
      .finally(() => {state.tableData.loading = false;})
  }).catch(() => {});
};

//-------------------------------------------------------------------
// 查询表格数据
const getTableData = () => {
  state.tableData.loading = true;
  const param = {
    keyword: state.keyword,
    pageNum: state.tableData.param.pageNum,
    pageSize: state.tableData.param.pageSize
  }
  api().list(param).then(res=>{
    if(commonUtil.hasErr(res, '查询' , false )) {return;}
    state.tableData.data = res.data.list;
    state.tableData.total = res.data.total;
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};

// 初始化数据
const initData = () => {
  // 初始化部门
  dept_api().list({nodeText: '顶级部门'}).then(res=>{
    state.dialog.deptOptions = [res.treeNode];
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {});

  // 初始化角色
  role_api().list({nodeText: '顶级角色'}).then(res=>{
    state.dialog.roleOptions = [res.treeNode];
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {});

}

//-------------------------------------------------------------------
// 页面加载时
onMounted(() => {
  getTableData();
  initData();
});

</script>


